﻿using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Controller.角度合格检测;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.View.多模板匹配;
using Halcon二次开发.View.相机;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.角度界面
{
    public partial class AnagleForm : Form
    {
        //角度合格检测对象
        public AngleAction controller;
        //模板文件路径
        public string shapeFilePath;
        //工具名称
        private string nodeName;
        public HwindowFlowNodeUrseControl hwindowFlowNode;

        public AnagleForm(string name, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.hwindowFlowNode = hwindowFlowNode;
            InitializeComponent();
            this.nodeName = name;
            controller = new AngleAction(hwindowFlowNode,this);
            //将窗口上功能默认是关闭的
            comboBox1.SelectedIndex = 1;
            comboBox5.SelectedIndex = 0;
            comboBox6.SelectedIndex = 0;
        }
        /// <summary>
        /// 反射调用存储窗体数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="nodes"></param>
        public void SetJsonPaths(string filePath, string addres)
        {
            //界面数据保存的路径
            string paths = filePath + "\\" + "界面数据\\" + nodeName + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
            try
            {
                string pmaPath = $@"Data\工程文件\{hwindowFlowNode.name}工程文件\模板匹配文件";
                if (Directory.Exists(pmaPath))
                {
                    string strs = Directory.GetFiles(pmaPath)[0];
                    if (strs != null)
                    {
                        shapeFilePath = strs;
                    }
                }
              controller.ReadFile();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":读取模板文件失败" + "\r\n");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains("模型匹配"))
                    {

                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }

                if (controller.HideIntputImage == null)
                {
                    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                    return;
                }
                //功能打开时进去
                if ((comboBox1.Text.Trim()=="打开"))
                {
                    controller.Run();
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                button2.Enabled = true;
                button3.Enabled = true;
                numericUpDown9.Enabled = true;

                comboBox5.Enabled = true;
                comboBox6.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;

                numericUpDown11.Enabled = true;
                numericUpDown14.Enabled = true;
                numericUpDown15.Enabled = true;
                numericUpDown12.Enabled = true;

                numericUpDown19.Enabled = true;
                numericUpDown17.Enabled = true;
                numericUpDown16.Enabled = true;
                numericUpDown18.Enabled = true;

                numericUpDown1.Enabled = true;
            }
            else
            {
                button2.Enabled = false;
                button3.Enabled = false;
                numericUpDown9.Enabled = false;
                comboBox5.Enabled = false;
                comboBox6.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;

                numericUpDown11.Enabled = false;
                numericUpDown14.Enabled = false;
                numericUpDown15.Enabled = false;
                numericUpDown12.Enabled = false;

                numericUpDown19.Enabled = false;
                numericUpDown17.Enabled = false;
                numericUpDown16.Enabled = false;
                numericUpDown18.Enabled = false;

                numericUpDown1.Enabled = false;
            }
        }
        /// <summary>
        /// 画一条基准线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            hwindowFlowNode.hSmartWindowControl1.SetFullImagePart();
            try
            {
                string pmaPath = $@"Data\工程文件\{hwindowFlowNode.name}工程文件\模板匹配文件";
                if (Directory.Exists(pmaPath))
                {
                    string strs = Directory.GetFiles(pmaPath)[0];
                    if (strs != null)
                    {
                        shapeFilePath = strs;
                    }
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":读取模板文件失败" + "\r\n");
            }
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains("模型匹配"))
                    {

                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }

                if (controller.HideIntputImage == null)
                {
                    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                    return;
                }
                //功能打开时进去
                if ((comboBox1.Text.Trim() == "打开"))
                {
                    controller.Draw_StandardLine();
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
            
        }
        /// <summary>
        /// 基准线的Run方法,测试方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Tool_FindStandardLine();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
           
        }
        /// <summary>
        /// 设置对位线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            hwindowFlowNode.hSmartWindowControl1.SetFullImagePart();
            try
            {
                //点击绘制时获取模板匹配数据
                foreach (TreeNode item in hwindowFlowNode.treeView1.Nodes)
                {
                    if (item.Name.Contains("模型匹配"))
                    {

                        controller.HideIntputArrayRows = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayRows;
                        controller.HideIntputArrayColumns = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayColumns;
                        controller.HideIntputArrayPhis = (item.Tag as H_PMA_S_AlignToolForm).controller.HideOutputArrayPhis;
                    }
                    else if (item.Name.Contains("相机"))
                    {
                        controller.HideIntputImage = (item.Tag as CamerForm).controller.HideOutputImage;
                    }
                }

                if (controller.HideIntputImage == null)
                {
                    (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + "当前图像为空!\r\n");
                    return;
                }
                //功能打开时进去
                if ((comboBox1.Text.Trim() == "打开"))
                {
                    controller.Draw_CounterpointLine();
                }
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 对位线测试方法，run
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Tool_FindCounterpointLine();

            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
    }
}
